package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author bairimengchang
 * @version 1.0
 * @project sky-take-out
 * @description 订单控制器
 * @date 2024/8/31
 */
@RestController("AdminOrderController")
@RequestMapping("/admin/order")
@Slf4j
@Api(tags = "订单相关接口")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 各个状态的订单数量统计
     * @return 订单统计视图对象
     */
    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数量统计")
    public Result<OrderStatisticsVO> statistics(){
        log.info("订单统计");
        OrderStatisticsVO orderStatisticsVO = orderService.statistics();
        return Result.success(orderStatisticsVO);
    }

    /**
     * 管理员取消订单
     * @param ordersCancelDTO 管理员取消订单传输数据模型
     * @return 取消订单操作结果信息
     */
    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public Result<String> cancelAdminOrder(@RequestBody OrdersCancelDTO ordersCancelDTO){
        log.info("取消订单：{}",ordersCancelDTO);
        boolean b = orderService.cancelAdminOrder(ordersCancelDTO);
        if (b){
            return Result.success("订单取消成功！");
        }
        return Result.error("取消订单失败！后端控制器报错！");
    }

    /**
     * 查询订单详情
     * @param id 订单id
     * @return 订单详情视图对象
     */
    @GetMapping("/details/{id}")
    @ApiOperation("查询订单详情")
    public Result<Object> getOrderDetail(@PathVariable("id") Long id){
        log.info("查询订单详情：{}",id);
        return Result.success(orderService.orderDetail(id));
    }

    /**
     * 完成订单
     * @param id 订单id
     * @return 完成订单操作结果信息
     */
    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result<String> completeOrder(@PathVariable("id") Long id){
        log.info("完成订单：{}",id);
        boolean b = orderService.completeOrder(id);
        if (b){
            return Result.success("订单完成！");
        }
        return Result.error("订单完成失败！后端控制器报错！");
    }

    /**
     * 拒单
     * @param ordersRejectionDTO 拒单传输数据模型
     * @return 拒单操作结果信息
     */
    @PutMapping("/rejection")
    @ApiOperation("拒单")
    public Result<String> rejectionOrder(@RequestBody OrdersRejectionDTO ordersRejectionDTO){
        log.info("拒单：{}",ordersRejectionDTO);
        boolean b = orderService.rejectionOrder(ordersRejectionDTO);
        if (b){
            return Result.success("拒单成功！");
        }
        return Result.error("拒单失败！后端控制器报错！");
    }

    /**
     * 接单
     * @param ordersConfirmDTO 接单传输数据模型
     * @return 接单操作结果信息
     */
    @PutMapping("/confirm")
    @ApiOperation("接单")
    public Result<String> confirmOrder(@RequestBody OrdersConfirmDTO ordersConfirmDTO){
        log.info("接单：{}",ordersConfirmDTO);
        boolean b = orderService.confirmOrder(ordersConfirmDTO);
        if (b){
            return Result.success("接单成功！");
        }
        return Result.error("接单失败！后端控制器报错！");
    }

    /**
     * 派送订单
     * @param id 订单id
     * @return 派单操作结果信息
     */
    @PutMapping("/delivery/{id}")
    @ApiOperation("派送订单")
    public Result<String> deliveryOrder(@PathVariable("id") Long id){
        log.info("派送订单：{}",id);
        boolean b = orderService.deliveryOrder(id);
        if (b){
            return Result.success("派送成功！");
        }
        return Result.error("派送失败！后端控制器报错！");
    }

    /**
     * 管理端分页查询订单信息及详情
     * @param ordersPageQueryDTO 条件搜索传输数据模型
     * @return 订单分页查询结果信息
     */
    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索")
    public Result<PageResult> getAllConditionSearch(OrdersPageQueryDTO ordersPageQueryDTO){
        // 和用户端不同，这里要查询全部订单
        log.info("订单搜索：{}",ordersPageQueryDTO);
        PageResult pageResult = orderService.getAllConditionSearch(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

}
